package com.bigdata.assignment.problem3;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class WordCountOptimizedCombiner extends Reducer<Text, IntWritable, Text, IntWritable> {

    private final IntWritable result = new IntWritable();

    public static enum COUNTERS {
        COMBINER_INPUT,
        COMBINER_OUTPUT
    }

    @Override
    public void reduce(Text key, Iterable<IntWritable> values, Context context)
            throws IOException, InterruptedException {

        int sum = 0;

        for (IntWritable v : values) {
            sum += v.get();
            context.getCounter(COUNTERS.COMBINER_INPUT).increment(1);
        }

        result.set(sum);
        context.write(key, result);

        context.getCounter(COUNTERS.COMBINER_OUTPUT).increment(1);
    }
}
